<?php

namespace Portal\TutorBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Portal\MessageBundle\Entity\Message;
use Portal\MessageBundle\Form\MessageType;
use Symfony\Component\HttpFoundation\Response;


class TutorController extends Controller
{
    public function showAction($id)
    {
        $tutor = $this->getDoctrine()->getEntityManager()->getRepository('PortalTutorBundle:Tutor')->getTutorInfo($id);
        
        $messageEntity = new Message();
        $form = $this->createForm(new MessageType($id, $this->container->getParameter('ROLE_TUTOR')), $messageEntity);        
        
        return $this->render('PortalTutorBundle:Tutor:show.html.twig', array(
            'id' => $id,
            'tutor' => $tutor,
            'form' => $form->createView()
            ));
    }
    
    public function myLessonAction()
    {
      $tutor = $this->getTutor();
      
      $lessonList = $this->getDoctrine()->getEntityManager()->getRepository('PortalTutorBundle:Tutor')->getTutorLesson($tutor->getId());
      return $this->render('PortalTutorBundle:Tutor:myLesson.html.twig', array(
                  'lessonList' => $lessonList,
              ));
    }
    
    public function myContractAction()
    {
      $tutor = $this->getTutor();

      $contractList = $this->getDoctrine()->getEntityManager()->getRepository('PortalTutorBundle:Tutor')->getTutorContract($tutor->getId());
      return $this->render('PortalTutorBundle:Tutor:myContract.html.twig', array(
                  'contractList' => $contractList,
              ));
    }
    
    public function myInvoiceAction()
    {
      $tutor = $this->getTutor();

      $invoiceList = $this->getDoctrine()->getEntityManager()->getRepository('PortalTutorBundle:Tutor')->getTutorInvoice($tutor->getId());  
      return $this->render('PortalTutorBundle:Tutor:myInvoice.html.twig', array(
                  'invoiceList' => $invoiceList,
              ));
    }
    
    private function getTutor()
    {
      $tutor = $this->container->get('security.context')->getToken()->getUser();

      // CHECK USER
      if (!is_object($tutor) || $tutor == 'anon.') {
        $this->get('session')->setFlash('error', 'Та нэвтэрч орно уу.');
        return $this->redirect($this->generateUrl('homepage'));
      }
      
      return $tutor;
      
    }
    
    public function tutorProfileAction()
    {
        return $this->render('PortalTutorBundle:Tutor:profile.html.twig', array(                  
              ));
    }
    
    
    public function confirmAction($hash)
    {
        $hash = \Portal\MainBundle\Utils\customFunctions::clear($hash);
        $hashObject = $this->getDoctrine()->getEntityManager()->getRepository('PortalTutorBundle:TutorConfirmHash')->findBy(array('hash' => $hash));
        $hashObject = $hashObject[0];
        $hashObject->setIsUsed(1);
        $hashObject->setUpdatedAt(new \DateTime());        
        $em = $this->getDoctrine()->getManager();
        $em->persist($hashObject);
        $em->flush();
        
        $tutorId = $hashObject->getTutorId();
        $tutor = $this->getDoctrine()->getEntityManager()->getRepository('PortalMainBundle:Tutor')->find($tutorId);        
        $tutor->setStep($this->container->getParameter('stepDone'));
        $em->persist($tutor);
        $em->flush();
        
        $this->get('session')->setFlash('notice', 'Амжилттай баталгаажууллаа та нэвтэрч орно уу.');
        header('Location: '.$this->container->getParameter('siteUrl').'/');
        exit;
        return new Response('');
//        return $this->redirect(
//                        $this->generateUrl("homepage", array())
//        );
    }
}
